package br.panthro.ui.media
{
	import br.panthro.media.video.VideoEvent;
	import br.panthro.media.video.VideoManager;
	import br.panthro.utils.color.Color;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.utils.clearInterval;
	import flash.utils.setInterval;
	
	public class VideoPlayer extends Sprite
	{
		private var _vHolder:Sprite = new Sprite();
		private var _videoContainer:Sprite;
		private var _trackingContainer:Sprite;
		private var _timeDisplay:TextField;
		private var _playPauseContainer:Sprite;
		private var _playButton:Sprite;
		private var _pauseButton:Sprite;
		private var _progressBar:Sprite;
		private var _progressBack:Sprite;
		private var _soundButton:Sprite;
		private var _fullScreenButton:Sprite;
		private var _curves:Sprite;
		
		private var _video:VideoManager;
		
		private var _videoPath:String;
		private var _colorCode:uint;
		private var _videoWidth:uint;
		private var _videoHeight:uint;
		private var _refreshInterval:Number;
		private var _useUI:Boolean = false;
		private var _autoPlay:Boolean = false;
		private var _maintainAspect:Boolean = true;
		
		public function VideoPlayer()
		{
			super();
		}
		
		public function setVideo(videoPath:String, colorCode:uint, videoWidth:uint, videoHeight:uint, useUI:Boolean = true, autoPlay:Boolean = false, maintainAspect:Boolean = true):void
		{
			_videoPath = videoPath;
			_colorCode = colorCode;
			_videoWidth = videoWidth;
			_videoHeight = videoHeight;
			_useUI = useUI;
			_autoPlay = autoPlay;
			_maintainAspect = maintainAspect;
			if(_useUI == true)
				createVideoUI();
			else 
				createVideo();
		}
		private function createVideoUI():void
		{
			_videoContainer = new Sprite();
			_videoContainer.graphics.beginFill(Color.BLUE, 0);
			_videoContainer.graphics.drawRect(0, 0, _videoWidth, _videoHeight);
			
			_videoContainer.graphics.endFill();
			
			addChild(_videoContainer);
			
			_videoContainer.addChild(_vHolder);
			
			_vHolder.addEventListener(MouseEvent.MOUSE_OVER, videoOver);
			_vHolder.addEventListener(MouseEvent.MOUSE_OUT, videoOut);
			
			_trackingContainer = new Sprite();
			_trackingContainer.graphics.beginFill(Color.WHITE, .3);
			_trackingContainer.graphics.drawRect(0, 0, _videoContainer.width - 40, 25);
			_trackingContainer.graphics.endFill();
			
			_trackingContainer.x = 20;
			_trackingContainer.y = _videoContainer.height -35;
			
			_videoContainer.addChild(_trackingContainer);
			
			
			var progressBarWidth:uint = (70*_trackingContainer.width) / 100;
			
			_progressBack = new Sprite();
			_progressBack.graphics.beginFill(Color.WHITE, 1);
			_progressBack.graphics.drawRect(0, 0, progressBarWidth, 10);
			
			_progressBack.x = 10; 
			_progressBack.y = 7;
			
			_trackingContainer.addChild(_progressBack);
			
			_progressBar = new Sprite();
			_progressBar.graphics.beginFill(_colorCode, 1);
			_progressBar.graphics.drawRect(0, 0, _progressBack.width, 10);
			
			_progressBack.addChild(_progressBar);
			
			var timeFormat:TextFormat = new TextFormat();
			
			timeFormat.font = "_sans";
			timeFormat.color = Color.WHITE;
			timeFormat.size = 10;
			_timeDisplay = new TextField();
			_timeDisplay.width = 70;
			_timeDisplay.height = 20;
			_timeDisplay.defaultTextFormat = timeFormat;
			
			_timeDisplay.text = "00:00 / 00:00";
			
			_timeDisplay.selectable = false;
			_timeDisplay.x = _progressBack.x + _progressBack.width + 5;
			_timeDisplay.y = 5
			
			addChild(createPlayPauseButton());
				
			_trackingContainer.addChild(soundButton());
			_trackingContainer.addChild(fullScreenButton());
			_trackingContainer.addChild(_timeDisplay);
			createVideo();
		}
		
		private function soundButton():Sprite
		{
			
			_soundButton = new Sprite();
			
			var speaker:Sprite = new Sprite();
			speaker.graphics.beginFill(Color.WHITE, 1);
			speaker.graphics.lineTo(0, 5);
			speaker.graphics.lineTo(5, 5);
			speaker.graphics.lineTo(7, 7);
			speaker.graphics.lineTo(7, -2);
			speaker.graphics.lineTo(5, 0);
			speaker.graphics.lineTo(0, 0);
			speaker.graphics.endFill();
			
			_curves = new Sprite();
			var smallCurve:Sprite = new Sprite();
			
			smallCurve.graphics.lineStyle(2, Color.WHITE, .8);
			smallCurve.graphics.curveTo(0, 0, 0, 0);
			smallCurve.graphics.curveTo(0, 10, -10, 7);
			smallCurve.rotation = -55
			smallCurve.scaleX = .5;
			smallCurve.scaleY = .5;
			
			smallCurve.x = 10;
			smallCurve.y -=1;
			
			var mediumCurve:Sprite = new Sprite();
			
			mediumCurve.graphics.lineStyle(2, Color.WHITE, .8);
			mediumCurve.graphics.curveTo(0, 0, 0, 0);
			mediumCurve.graphics.curveTo(0, 10, -10, 7);
			mediumCurve.rotation = -55
			mediumCurve.scaleX = .7;
			mediumCurve.scaleY = .7;
			
			mediumCurve.x = 12;
			mediumCurve.y -=2;
			
			var bigCurve:Sprite = new Sprite();
			
			bigCurve.graphics.lineStyle(2, Color.WHITE, .8);
			bigCurve.graphics.curveTo(0, 0, 0, 0);
			bigCurve.graphics.curveTo(0, 10, -10, 7);
			bigCurve.rotation = -55
			bigCurve.scaleX = .9;
			bigCurve.scaleY = .9;
			
			bigCurve.x = 14;
			bigCurve.y -=3;
			
			_curves.addChild(smallCurve);
			_curves.addChild(mediumCurve);
			_curves.addChild(bigCurve);
			
			var hit:Sprite = new Sprite();
			hit.graphics.beginFill(Color.WHITE, 0);
			hit.graphics.drawRect(0, 0, 20, 15);
			hit.graphics.endFill();
			hit.y -=5;
			
			_soundButton.addChild(_curves);
			
			_soundButton.addChild(speaker);
			_soundButton.addChild(hit);
			
			_soundButton.x = _timeDisplay.x + 75;
			_soundButton.y = 10;
			
			return _soundButton;
		}
		
		private function fullScreenButton():Sprite
		{
			_fullScreenButton = new Sprite();
			
			for(var i:int = 0; i!= 4; i++)
				_fullScreenButton.addChild(createArrow(i*90));
			
			var hit:Sprite = new Sprite();
			hit.graphics.beginFill(Color.WHITE, 0);
			hit.graphics.drawRect(0, 0, 30, 30);
			hit.graphics.endFill();
			
			_fullScreenButton.addChild(hit);
			_fullScreenButton.scaleX = _fullScreenButton.scaleY = .5
			
			_fullScreenButton.x = _soundButton.x + 35;
			_fullScreenButton.y = 5;
			return _fullScreenButton;
		}
		private function createArrow(degree:int):Sprite
		{
			var arrow:Sprite = new Sprite();
			
			arrow.graphics.beginFill(Color.WHITE, 1)
			arrow.graphics.lineTo(0, 10);
			arrow.graphics.lineTo(3, 7);
			arrow.graphics.lineTo(7, 10);
			arrow.graphics.lineTo(10, 7);
			arrow.graphics.lineTo(7, 3);
			arrow.graphics.lineTo(10, 0);
			arrow.graphics.endFill();
			arrow.rotation = degree
			if(degree == 90)
			{
				arrow.x = 30;
				arrow.y = 0;
			}
			if(degree == 180)
			{
				arrow.x = 30;
				arrow.y = 30;
			}
			if(degree == 270)
			{
				arrow.x = 0;
				arrow.y = 30;
				
			}
			
			return arrow;
		}
		private function createPlayPauseButton():Sprite
		{
			_playPauseContainer = new Sprite();
			_playPauseContainer.x = (_videoWidth / 2) - 45;
			_playPauseContainer.y = (_videoHeight / 2) - 45;
			_playPauseContainer.graphics.beginFill(_colorCode, 1);
			_playPauseContainer.graphics.drawRoundRect(0, 0, 95, 95, 15, 15);
			_playPauseContainer.graphics.endFill();
			
			_playButton = new Sprite();
			_playButton.graphics.beginFill(Color.WHITE, 1);
			_playButton.graphics.lineTo(0, 40);
			_playButton.graphics.lineTo(35, 20);
			_playButton.graphics.lineTo(0,0);
			_playButton.graphics.endFill();
			
			_pauseButton = new Sprite();
			_pauseButton.graphics.beginFill(Color.WHITE, 1);
			_pauseButton.graphics.drawRect(0, 0, 20, 60);
			_pauseButton.graphics.drawRect(40, 0, 20, 60);
			_pauseButton.graphics.endFill();
			
			_pauseButton.x = 20;
			_pauseButton.y = 20;
			_pauseButton.visible = false;
			
			_playButton.x = 35;
			_playButton.y = 30;
			
			_playPauseContainer.addChild(_pauseButton);
			_playPauseContainer.addChild(_playButton);
			
			
			return _playPauseContainer;
		}
		private function createVideo():void
		{
			
			if(!_videoContainer)
			{
				
				_videoContainer = new Sprite();
				_videoContainer.addChild(_vHolder);
				addChild(_videoContainer);
			}
			_video = new VideoManager();
			_vHolder.addChild(_video);
			_video.setVideo(_videoPath, _videoHeight, _videoWidth, _autoPlay, true);
			
			_video.addEventListener(VideoEvent.LOAD_COMPLETE, videoLoaded);
			_video.addEventListener(VideoEvent.LOAD_ERROR, videoError);
			
		}
		private function videoLoaded(event:VideoEvent):void
		{
			
			
			
			_video.addEventListener(VideoEvent.VIDEO_PLAYING, videoPlaying);
			_video.addEventListener(VideoEvent.VIDEO_PAUSED, videoPaused);
			_video.addEventListener(VideoEvent.VIDEO_STOPPED, videoStopped);
			
			if(_useUI == true)
			{
				_progressBar.scaleX = 0;
			
				_playPauseContainer.addEventListener(MouseEvent.CLICK, playButtonHandler);
				_playPauseContainer.addEventListener(MouseEvent.MOUSE_OUT, playPauseOut);
				_playPauseContainer.addEventListener(MouseEvent.MOUSE_OVER, playPauseOver);
				_playPauseContainer.buttonMode = true;
				_soundButton.addEventListener(MouseEvent.CLICK, soundButtonHandler);
				_soundButton.buttonMode = true;
				_fullScreenButton.addEventListener(MouseEvent.CLICK, fullScreenButtonHandler);
				_fullScreenButton.buttonMode = true;
			}
			
			dispatchEvent(new VideoEvent(VideoEvent.LOAD_COMPLETE));
		}
		private function videoError(event:VideoEvent):void
		{	
			dispatchEvent(new VideoEvent(VideoEvent.LOAD_ERROR));
		}
		private function playPauseOver(event:MouseEvent):void
		{
			_playPauseContainer.alpha = 1;
		}
		private function playPauseOut(event:MouseEvent):void
		{
			_playPauseContainer.alpha = 0;
		}
		private function playButtonHandler(event:MouseEvent):void
		{
			if(_video.status == "paused" || _video.status == "stopped")
			{
				_playButton.visible = false;
				_pauseButton.visible = true;
				_video.play();
				_refreshInterval = setInterval(refreshDisplay, 500);
			} else { 
				clearInterval(_refreshInterval);
				_playButton.visible = true;
				_pauseButton.visible = false;
				_video.pause();
			}
			_timeDisplay.text = formatedTime(_video.currentTime) + " / " + formatedTime(_video.duration)
		}
		private function soundButtonHandler(event:MouseEvent):void
		{
			
			if(_video.volume == 1)
			{
				_video.mute();
			} else
			{
				_video.unmute();
			}
		}
		private function refreshDisplay():void
		{
			_timeDisplay.text = formatedTime(_video.currentTime) + " / " + formatedTime(_video.duration);
			_progressBar.scaleX = _video.percentage/100;
		}
		private function fullScreenButtonHandler(event:MouseEvent):void
		{
			if(stage["displayState"]=="normal"){
				stage["displayState"]="fullScreen";
			}else{
				stage["displayState"]="normal";
			}
		}
		
		private function videoOver(event:MouseEvent):void
		{
			_playPauseContainer.alpha = 1;	
		}
		private function videoOut(event:MouseEvent):void
		{
			_playPauseContainer.alpha = 0;
		}
		private function videoPlaying(event:VideoEvent):void
		{
			dispatchEvent(new VideoEvent(VideoEvent.VIDEO_PLAYING));
		}
		private function videoPaused(event:VideoEvent):void
		{
			dispatchEvent(new VideoEvent(VideoEvent.VIDEO_PAUSED));	
		}
		private function videoStopped(event:VideoEvent):void
		{
			dispatchEvent(new VideoEvent(VideoEvent.VIDEO_STOPPED));
		}
		private function formatedTime(time:Number):String	
		{
			
			time=Math.round(time);
			
			var seconds:Number = Math.floor(time % 60);
			var minutes:Number = Math.floor(time / 60);
			
			var sec:String = seconds.toString();
			var min:String = minutes.toString();
			
			if(sec.length < 2)
					sec = '0'+sec;

			if(min.length < 2)
				min = '0'+min;
			
			var timeCode:String = min+":"+sec;
			
			return timeCode;
		}
		public function get video():VideoManager
		{
			return _video;
		}
	}
}